package cn.tedu.blackhorse.service.impl;


import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.JobMapper;
import cn.tedu.blackhorse.mapper.TeacherMapper;
import cn.tedu.blackhorse.pojo.dto.JobAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.JobUpdateDTO;
import cn.tedu.blackhorse.pojo.entity.Job;
import cn.tedu.blackhorse.pojo.vo.JobListItemVO;
import cn.tedu.blackhorse.pojo.vo.JobStandardVO;
import cn.tedu.blackhorse.service.IJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class JobServiceImpl implements IJobService {

    @Autowired
    private JobMapper jobMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void insertJob(JobAddNewDTO jobAddNewDTO) {
        int count = jobMapper.countByName(jobAddNewDTO.getJobName());
        if (count != 0){
            String message = "添加职位失败,所添加的职位已存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Job job = new Job();
        BeanUtils.copyProperties(jobAddNewDTO,job);
        int row = jobMapper.insert(job);
        if (row != 1){
            String message = "添加失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteJob(Long id) {
        JobStandardVO jobStandardVO = jobMapper.getById(id);
        if (jobStandardVO == null){
            String message = "删除失败,要删除的职务不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int count = teacherMapper.countByJobId(id);
        if (count != 0){
            String message = "删除失败,所删除的职务存在关联的教师";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        int row = jobMapper.deleteById(id);
        if (row != 1){
            String message = "删除失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateJob(Long id ,JobUpdateDTO jobUpdateDTO) {
        JobStandardVO jobStandardVO = jobMapper.getById(id);
        if (jobStandardVO == null) {
            String message = "修改失败,所修改的数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Job job = new Job();
        BeanUtils.copyProperties(jobUpdateDTO,job);
        job.setId(id);
        int row = jobMapper.updateById(job);
        if (row != 1){
            String message = "修改失败,系统繁忙,请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public List<JobListItemVO> list() {
        return jobMapper.list();
    }

    @Override
    public JobStandardVO getById(Long id) {
        JobStandardVO jobStandardVO = jobMapper.getById(id);
        if (jobStandardVO == null){
            String message = "查询失败,所查询的职责不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return jobStandardVO;
    }
}
